import { ref, watch } from "vue";
import type { BaseWidgetType } from "../type";

export const useRules = (widget:BaseWidgetType,state:any)=>{
    const addRule = ()=>{
        const rule:{required:boolean,message:string} = {
            required:false,
            message:""
        }
        rule.required = widget.options.required
        rule.message = widget.options.requiredHint
        if(rule.required){
            state.rules.push(rule)
        }
    }

    const clear = ()=>{
        state.rules = []
    }
    watch(
        [()=>widget.options.required],
        ()=>{
            if(widget.options.required){
                addRule()
            }else {
                clear()
            }
        },
        {
            deep:true,
            immediate:true
        }
    )
}